package com.lzh.shijicloudstack.manager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class SysUtilManager {
    public SysUtilManager() {
    }

    public static String getNextId() {
        return UUIDService.getInstance().simpleHex();
    }

    public static String getProjectId() {
        StringBuilder result = new StringBuilder();
        result.append(System.currentTimeMillis());
        result.append(String.format("%07d", (new Random()).nextInt(9999999)));
        return result.toString();
    }

    public static String getVirtualId() {
        StringBuilder result = new StringBuilder();
        result.append(System.currentTimeMillis());
        result.append(String.format("%07d", (new Random()).nextInt(9999999)));
        return result.toString();
    }

    public static String getRandomNum(int max) {
        int maxS = String.valueOf(max).length();
        return String.format("%0" + maxS + "d", (new Random()).nextInt(max));
    }

    public static String getAssetInfoId() {
        return UUIDService.getInstance().simpleHex();
    }

    public static String getAssetId(String sn, String type, String prefix, int count) {
        String countString = Integer.toString(count);
        int length = countString.length();

        for(int i = 0; i < 6 - length; ++i) {
            countString = "0" + countString;
        }

        return "0".equals(type) ? sn + countString : prefix + sn + countString;
    }

    public static String getMaintenanceId(int count) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        StringBuilder result = new StringBuilder();
        result.append(sdf.format(date));
        String countString = Integer.toString(count + 1);
        int length = countString.length();

        for(int i = 0; i < 4 - length; ++i) {
            countString = "0" + countString;
        }

        result.append(countString);
        return result.toString();
    }

    public static String sharedECUSeparate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        String str = sdf.format(new Date());
        Random random = new Random();
        String randoms = "";

        for(int i = 0; i < 4; ++i) {
            randoms = random.nextInt(10) + randoms;
        }

        return "00" + str + randoms;
    }

    public static String generateCompanyCode(String areaString, Date createDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        char[] ch = "0ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
        int area = Integer.parseInt(areaString);
        String createDateString = sdf.format(createDate);
        String year = createDateString.substring(2, 4);
        int month = Integer.parseInt(createDateString.substring(4, 6));
        String company_code = ch[area] + year + ch[month];
        if (company_code.length() == 4) {
            String randomNum = generateRandomNumbers(4);
            company_code = company_code + randomNum;
            return company_code;
        } else {
            return null;
        }
    }

    public static String generateRandomNumbers(int num) {
        int max = (int)Math.pow((double)10.0F, (double)num);
        return String.valueOf((new Random()).nextInt(max) + max).substring(1, num + 1);
    }

    public static String getOpenId(String openId) {
        return Encryption.SpecificEncryption(openId, "salt");
    }
}
